<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/1/22
 * Time: 14:24
 */
namespace App\Http\Services;

use App\Common\Config;
use App\Common\AppConst;
use App\Common\AppFunction;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query;
class ClientService {

    /*
     * 用户登录
     */
    public function login($username,$pwd){

        $result = DB::table('fcz_members')->where('username',$username)->orwhere('username',$username)->select('uid','nickname','password','salt')->first();
        if(empty($result)){
            return false;
        }
        $pwd = md5(md5($pwd).$result->salt);
        if($pwd != $result->password){
            return false;
        }
        unset($result->password);
        unset($result->salt);
        return $result;
    }
    /*
     * 获取用户注册地址
     */
    public function reg(){
        $result = array("reg_url"=>"http://www.fcz.cn/reg","open"=>1);
        return $result;
    }
    /*
    * 获取官网地址
    */
    public function website(){
        $result = array(array('name'=>'浏览官网','url'=>'http://www.fz.cn','open'=>1),array('name'=>'出售效果图','url'=>'http://www.fz.cn','open'=>1));
        return $result;
    }
    /*
    * 关于我们
    */
    public function about()
    {
        $result = array(array('name'=>'标题','desc'=>'内容'),
            array('name'=>'版权','desc'=>'MAX管家素材管理系统 ® Version 3.2\r\nCopyright © 2013-2017 HTTP://MAXGJ.NET 版权所有\r\n任何单位及个人不得以任何理由进行或尝试软件破解、反编、非法传播,违者必究。\r\nMAX管家网保留所有解释权'),
            array("name"=>"授权","desc"=>"免费授权给 个人用户 使用权限 ") );
        return $result;
    }
    /*
     * 获取导航栏目
     */
    public function nav(){
        $result = array(
            array(
            "name"=>"单体模型",
            "type"=>100,
            "icon"=>"http://www.fcz.cn/icon.png",
            "category_url"=>"",
            "content_url"=>""
            ),
            array(
            "name"=>"材质",
            "type"=>200,
            "icon"=>"http://www.fcz.cn/icon.png",
            "category_url"=>"",
            "content_url"=>""
            ),
            array(
            "name"=>"灯光",
            "type"=>300,
            "icon"=>"http://www.fcz.cn/icon.png",
            "category_url"=>"",
            "content_url"=>""
            ),
            array(
            "name"=>"脚本",
            "type"=>400,
            "icon"=>"http://www.fcz.cn/icon.png",
            "category_url"=>"",
            "content_url"=>""
            ),
            array(
            "name"=>"贴图",
            "type"=>500,
            "icon"=>"http://www.fcz.cn/icon.png",
            "category_url"=>"",
            "content_url"=>""
            ),
            array(
            "name"=>"CAD",
            "type"=>600,
            "icon"=>"http://www.fcz.cn/icon.png",
            "category_url"=>"",
            "content_url"=>""
            )
        );

        return $result;
    }
    /*
     * 获取分类
     */
    public function getCategory($type,$category){
        $result = DB::table('fcz_models_categories')->where('parent_id',$type)->select('category_id','parent_id','category_name')->get();
        if(empty($result)){
            return  (object)array();
        }
        foreach($result as $val){
            $oneId[] = $val->category_id;    //子类的ids
        }
        $result2 = DB::table('fcz_models_categories')->wherein('parent_id',$oneId)->select('category_id','parent_id','category_name')->get();
        //导航(id=$type) 下一级分类
        if(empty($result2)){
            foreach($result as $val){
                $val->children = array();
            }
            return $result;
        }
        //导航(id=$type) 下二级分类category_id
        $twoIdArr = my_array_column($result2,'category_id');
        //三级分类
        $result3 = DB::table('fcz_models_categories')->wherein('parent_id',$twoIdArr)->select('category_id','parent_id','category_name')->get();
        foreach($result as $val){
            $children=array();
            foreach($result2 as $val2){
                $children2=array();
                foreach ($result3 as $val3) {
                    if ($val3->parent_id == $val2->category_id) {
                        $children2[] = $val3;
                    }
                }
                $val2->children = $children2;

                if($val2->parent_id == $val->category_id){
                    $children[]=$val2;
                }
            }
            $val->children = $children;
        }
        return $result;
    }
    /*
     * 获取模型风格
     */
    public function getStyle($type=100,$style){
        $result=DB::table('fcz_options')->select('name','option_id','value')->where('name','models_style')->get();
        if(empty($result)){
            return array();
        }
        foreach($result as $val){
            $val->name = $val->value;
            $val->id = $val->option_id;
            unset($val->value);
            unset($val->option_id);
        }
        return $result;
    }
    /*
     * 获取模型支持格式(max)版本
     */
    public function getFormat($type=1){
        $result=DB::table('fcz_options')->select('name','option_id','value')->where('name','models_format')->get();
        if(empty($result)){
            return array();
        }
        foreach($result as $val){
            $val->name = $val->value;
            $val->id = $val->option_id;
            unset($val->value);
            unset($val->option_id);
        }
        return $result;
    }
    /*
     * 获取模型/材质/灯光/脚本/贴图/CAD 内容
     */
    public function getList($type,$rows=30){
        $tableName = '';
        $result=DB::table('fcz_source_categories')->where('type',$type)->select('category_id','category_name','type')->get();
        if(empty($result)){
            return array();
        }
        $oneIds = my_array_column($result,'category_id');
        if($type == 1)  $tableName = 'fcz_source_chartlet';
        if($type == 2)  $tableName = 'fcz_source_vray';
        if($type == 4)  $tableName = 'fcz_source_max';
        if($tableName != ''){
            $resultTwo=DB::table($tableName)->wherein('category_id',$oneIds)->select('id','img','title','file_path','category_id','keyword')->get();
        }

        if($type == 3){
            $tableName = 'fcz_source_optic_net';
            $resultTwo=DB::table($tableName)->wherein('category_id',$oneIds)->select('id','title','file_path','category_id','keyword')->get();
        }

        foreach($result as $val){
            foreach($resultTwo as $valTwo){
                if($val->category_id == $valTwo->category_id){
                    $valTwo->category_name = $val->category_name;
                    if(isset($valTwo->img))
                        $valTwo->img = Config::FCZ_ICON_PREFIX.$valTwo->img;
                }
            }
        }
        $countData = count($resultTwo,COUNT_NORMAL);
        $result = array('total'=>$countData,'per_page'=>'30','current_page'=>'','last_page'=>'','rows'=>$resultTwo);
        return $result;
    }

}


